/*
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2021-08-12 18:16:19
 * @LastEditors: GGB
 * @LastEditTime: 2021-08-15 21:24:08
 */
import 'package:flutter/material.dart';

class AnimText extends StatefulWidget {
  final int number;
  final int duration;
  final TextStyle style;

  const AnimText({
    Key key,
    this.number,
    this.duration = 1500,
    this.style,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return AnimState();
  }
}

class AnimState extends State<AnimText> with SingleTickerProviderStateMixin {
  AnimationController controller;
  Animation animation;
  var begin = 0;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
        vsync: this, duration: Duration(milliseconds: widget.duration));
  }

  @override
  Widget build(BuildContext context) {
    if (widget.number != 0) {
      final Animation curve =
          CurvedAnimation(parent: controller, curve: Curves.linear);
      setState(() {
        animation = IntTween(begin: begin, end: widget.number).animate(curve)
          ..addStatusListener((status) {
            if (status == AnimationStatus.completed) {
              // controller.reverse();
            }
          });
      });
      controller.forward();
    }
    return widget.number != 0
        ? AnimatedBuilder(
            animation: controller,
            builder: (context, child) {
              return Container(
                child: Text(
                  "${animation.value}",
                  style: widget.style,
                ),
              );
            },
          )
        : Text(
            "0",
            style: widget.style,
          );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}
